package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.itims.bean.UcAppBean;
import com.sdhs.paas.itims.dto.UcAppDTO;
import com.sdhs.paas.itims.dto.req.UcAppReqDTO;
import com.sdhs.paas.itims.dto.res.UcAppResDTO;
import com.sdhs.paas.itims.service.UcAppService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

@RefreshableRestController
@RequestMapping(value = "/app")
public class UcAppController {

	@Autowired
	private UcAppService ucAppService;

	@PostMapping("/getPageAppList")
	public PaasBaseResponse<UcAppResDTO> getUcAppPageList(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO) {
		// 将dto转为bean
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(ucAppReqDTO.getBody().getUcAppDTO(),
				UcAppBean.class);
		// 获取前端传递的分页参数
		PageInfoDTO pageInfoDTO = ucAppReqDTO.getBody().getPageInfo();
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		Page<UcAppBean> page = ucAppService.getPageUcAppList(pageRequest, ucAppBean);

		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		PageInfoDTO resPageInfoDTO = new PageInfoDTO();
		resPageInfoDTO.setPage(page.getCurPage());
		resPageInfoDTO.setPageSize(page.getPageSize());
		resPageInfoDTO.setPageCount(page.getPageCount());
		resPageInfoDTO.setRowCount(page.getTotalCount());
		ucAppResDTO.setPageInfo(resPageInfoDTO);
		List<UcAppBean> ucAppBeanList = page.getResult();
		List<UcAppDTO> dtoList = new ArrayList<UcAppDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(ucAppBeanList, new TypeReference<List<UcAppDTO>>() {
		});
		ucAppResDTO.setUcAppDTOList(dtoList);
		return new PaasBaseResponse<>(ucAppResDTO);
	}

//	@PostMapping("/getUcAppList")
//	public PaasBaseResponse<TreeResDTO> getUcAppList(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO) {
//		// 将dto转为bean
//		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(ucAppReqDTO.getBody().getUcAppDTO(),
//				UcAppBean.class);
//		List<UcAppBean> ucAppBeanList = ucAppService.getUcAppList(ucAppBean);
//		List<TreeDTO> treeList = new ArrayList<TreeDTO>();
//		if(ucAppBeanList != null && ucAppBeanList.size()>0){
//			TreeDTO treeDTO = null;
//			List<TreeDTO> nullList = new ArrayList<TreeDTO>();
//			for(UcAppBean appbean : ucAppBeanList){
//				treeDTO = new TreeDTO();
////				treeDTO.setId(appbean.getAppId());
//				treeDTO.setId(appbean.getId());
//				treeDTO.setLabel(appbean.getAppName());
//				treeDTO.setChildren(nullList);
//				treeList.add(treeDTO);
//			}
//		}
//		TreeResDTO treeResDTO = new TreeResDTO();
//		treeResDTO.setTreeDTOList(treeList);
//		return new PaasBaseResponse<>(treeResDTO);
//	}

	@PostMapping("/saveUcApp")
	public PaasBaseResponse<UcAppResDTO> saveUcApp(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO) {
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//		ucAppBean.setAppId(uuid);
		ucAppBean.setId(uuid);
		ucAppBean.setCtime(System.currentTimeMillis());
		ucAppBean.setFlag(0);
		ucAppBean.setUtime(System.currentTimeMillis());
		Boolean flag = ucAppService.saveUcApp(ucAppBean);
		appDTO.setAppId(uuid);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}

	@PostMapping("/updateUcApp")
	public PaasBaseResponse<UcAppResDTO> updateUcApp(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO) {
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
		ucAppBean.setUtime(System.currentTimeMillis());
		Boolean flag = ucAppService.updateUcApp(ucAppBean);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}

	@PostMapping("/getUcAppDetail")
	public PaasBaseResponse<UcAppResDTO> getUcAppDetail(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO) {
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
//		ucAppBean = ucAppService.getUcAppDetail(ucAppBean.getAppId());
		ucAppBean = ucAppService.getUcAppDetail(ucAppBean.getId());
		appDTO = EntityDtoConvertUtil.convertToDTO(ucAppBean, UcAppDTO.class);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}
}
